<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Job extends MY_Admin
{

    protected $data;

    function __construct()
    {
        parent::__construct();
        
        $this->load->model(array('category_model','job_model', 'field_model'));
        $this->load->library('MY_FormParam');

        $arrCategory = $this->category_model->getCategoryList();
        $this->data['category'] = MY_Helper::convArray($arrCategory, 'category_id', 'category_name');
        
        $category_id = $this->input->post('category_id');
        $this->data['field'] = $this->field_model->getFieldByCategoryID($category_id);
    }

    function index(){
        $objFormParam = $this->my_formparam;
        $this->initSearchParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrForm']   = $objFormParam->getFormParamList();
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        
        $mode = $this->input->post('mode');
        switch ($mode){
            case 'delete':
                $job_id = $this->input->post('job_id');
                if(!$this->delete('job', array('job_id' => $job_id))){
                    $this->data['msg'] = '削除が失敗しました。';
                } else {
                    $this->data['msg'] = '削除が完了しました。';
                }
            case 'search':
                $validate = $this->job_model->exec_validation('job_search');
                if($validate['status'] == FALSE){
                    $this->data['arrErr']  = $validate['data'];
                    return $this->view('admin/job_list', $this->data);
                }
            default:
                $params = $objFormParam->getHashArray();
                $pager = array(
                    'cur_page'     => $this->input->post('search_pageno'),
                    'total_rows' => count($this->job_model->getJobList($params)),
                );
                $this->setPager($pager);
                $per_page = $this->pager['per_page'];
                $offset   = $this->offset();

                $this->data['job']     = $this->job_model->getJobList($params, true, $per_page, $offset, 'job.lastup_datetime DESC');
                $this->data['job_cnt'] = $this->pager['total_rows'];
                $this->data['pager']   = $this->getPagerTpl();
                break;
        }
	   $this->view('admin/job_list', $this->data);
    }
    function edit(){
        $objFormParam = $this->my_formparam;
        $this->initSearchParam($objFormParam);
        $this->initFormParam($objFormParam);
        
        $objFormParam->setParam($_POST);
        $objFormParam->trimParam();
        
        $this->data['arrHidden'] = $objFormParam->getSearchArray();
        $this->data['arrForm'] = $objFormParam->getFormParamList();
        
        $this->setUploadConfig();
        $mode = $this->input->post('mode');
        switch ($mode)
        {
            case "pre_edit":
                $job_id = $this->input->post('job_id');
                $arrData = $this->job_model->getJobById($job_id);
                $objFormParam->setParam($arrData);
                
                $this->data['field']   = $this->field_model->getFieldByCategoryID($arrData['category_id']);
                $this->data['arrForm'] = $objFormParam->getFormParamList();
                $this->data["job_id"]  = $job_id;
                $template = 'admin/job_edit';
                break;
            case "complete":
                $validate = $this->job_model->exec_validation('job_edit');
                if($validate['status'] == FALSE){
                    $this->data['arrErr']  = $validate['data'];
                }

                $params = $objFormParam->getDbArray();
                if($_FILES['logo']['error'] == 0) {
                    unset($this->data['arrErr']['logo']);
                }
                $upload_flg = array();
                $uploadData = MY_Helper::getUploadData();
                foreach ($uploadData as $key => $value) {
                    if($value['error'] != 0){
                        continue;
                    }
                
                    if (! $this->upload->do_upload($key, date('Ymd') . '_' . uniqid(''))) {
                        $this->data['arrErr'][$key]['error'] = $this->upload->client_name.' '.$this->upload->error_msg[0];
                        $this->upload->error_msg = array();
                        $params[$key] = '';
                        $params["save_$key"] = '';
                    } else {
                        $params[$key] = $value['name'];
                        $params["save_$key"] = $this->upload->file_name;
                    }
                    $upload_flg[$key] = true;
                }
                
                if(!empty($this->data['arrErr'])){
                    $objFormParam->setParam($params);
                    $this->data['arrForm'] = $objFormParam->getFormParamList();
                    $this->data['upload_flg'] = $upload_flg;
                    return $this->view('admin/job_edit', $this->data);
                }
                
                if(!$this->editJob($params)) {
                    $this->data["msg"] = "登録が失敗しました。";
                    return $this->view('admin/job_edit',$this->data);
                }

                $tmp_dir = BASEDIR . 'photo_tmp/';
                $save_dir = BASEDIR . 'photo/';
                MY_Helper::saveFile($params['save_logo'], $tmp_dir, $save_dir);
                for($i = 1; $i<=3; $i++){
                    if(!empty($params['save_sub_image'.$i])) {
                        MY_Helper::saveFile($params['save_sub_image'.$i], $tmp_dir, $save_dir);
                    }
                }

                $this->data["msg"]     = "登録が完了致しました。";
                $this->data['arrForm'] = null;
                $template = 'admin/job_complete';
                break;
            default:
                $template = 'admin/job_edit';
                break;
        }
        $this->view($template, $this->data);
    }


    public function initFormParam(&$objFormParam)
    {
        $objFormParam->addParam('仕事ID', 'job_id');
        $objFormParam->addParam('仕事コード', 'job_code');
        $objFormParam->addParam('地域', 'pref_id');
        $objFormParam->addParam('カテゴリ', 'category_id');
        $objFormParam->addParam('フィールド', 'field_id', array());
        $objFormParam->addParam('経験', 'experience');
        $objFormParam->addParam('給与', 'salary');
        $objFormParam->addParam('状態', 'status');
        $objFormParam->addParam('タイトル', 'content_title');
        $objFormParam->addParam('業種', 'content_industry');
        $objFormParam->addParam('職種', 'content_category');
        $objFormParam->addParam('給与内容', 'content_salary');
        $objFormParam->addParam('勤務地', 'content_location');
        $objFormParam->addParam('必要な日本語力', 'content_jp_skill');
        $objFormParam->addParam('必要な英語力', 'content_en_skill');
        $objFormParam->addParam('資格その他必要条件', 'content_require');
        $objFormParam->addParam('雇用期間', 'content_contract');
        $objFormParam->addParam('会社の従業員数', 'content_company_size');
        $objFormParam->addParam('外国人を採用したことの有無', 'content_status_adopt');
        $objFormParam->addParam('その他公開条件', 'content_others');
        
        $objFormParam->addParam('ロゴ', 'logo');
        $objFormParam->addParam('ロゴ', 'save_logo');
        
        for($i = 1; $i<=3; $i++){
            $objFormParam->addParam('画像'.$i, 'sub_image'.$i);
            $objFormParam->addParam('画像'.$i, 'save_sub_image'.$i);
        }
        
        $objFormParam->addParam('掲載日', 'regist_datetime');
    }
    public function initSearchParam(&$objFormParam)
    {
        $objFormParam->addParam('仕事ID', 'job_id');
        $objFormParam->addParam('仕事コード', 'search_job_code','',false);
        $objFormParam->addParam('タイトル', 'search_job_title','',false);
        $objFormParam->addParam('カテゴリ', 'search_category_id','',false);
//         $objFormParam->addParam('Field id', 'search_field_id','',false);
        $objFormParam->addParam('地域', 'search_pref_id','',false);
        $objFormParam->addParam('経験', 'search_experience','',false);
        $objFormParam->addParam('給与01', 'search_salary_from','',false);
        $objFormParam->addParam('給与02', 'search_salary_to','',false);
//         $objFormParam->addParam('状態', 'search_status','',false);
        $objFormParam->addParam('Display Search Form', 'search_form_display','',false);
        $objFormParam->addParam('PageMax', 'search_page_max','',false);
        $objFormParam->addParam('PageNo', 'search_pageno','',false);
//         $objFormParam->addParam('Create Date', 'search_create_date_from','',false);
//         $objFormParam->addParam('Create Date', 'search_create_date_to','',false);
    }
    
    public function editJob($params = array())
    {
        if (empty($params)){
            return false;
        }
        $field_id = $params['field_id'];
        $params['field_id'] = 0;
        if(is_array($field_id) && !empty($field_id)) {
            $params['field_id'] = implode('-', $field_id);
        }
        $job_id = $params['job_id'];
        unset($params['job_id']);
        if(is_numeric($job_id)){
            if(!$this->update('job', $params, array('job_id' => $job_id))) {
                return false;
            }
        } else {
            if(!$this->insert('job', $params)) {
                return false;
            }
        }
        return true;
    }
    
    public function setUploadConfig()
    {
        if(!file_exists(BASEDIR . 'photo/')) {
            mkdir(BASEDIR . 'photo/', 0777);
        }
    
        if(!file_exists(BASEDIR . 'photo_tmp/')) {
            mkdir(BASEDIR . 'photo_tmp/', 0777);
        }
    
        $uploadCnf = $this->config->item('upload');
        $uploadCnf['upload_path']   = BASEDIR . 'photo_tmp/';
        $uploadCnf['allowed_types'] = 'png|gif|bmp|jpg|jpeg';
        $this->load->library('upload', $uploadCnf);
    }
}
